<template>
	<view>
		<mc-framework navTitle="MES工控机" @changeLevel="changeLevel">
			<template #content>
				<view class="content" style="height: 100%;">
					<view class="contentBox">
						<view class="container">
							<uni-forms ref="baseForm" :modelValue="baseFormData">
								<view class="form-item-row">

									<uni-forms-item label="派工单编号:" label-width="auto">
										<input v-model="baseFormData.PGBillNo" type="text" placeholder="自动带出派工单编号"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="开工编号:" label-width="auto">
										<input v-model="baseFormData.BillNO" type="text" placeholder="自动带出开工编号"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="开工日期:" label-width="auto">
										<input v-model="baseFormData.StartSingleDate" disabled type="text"
											placeholder="自动带出开工日期" />
										<text class="text" @click="show=true"></text>

									</uni-forms-item>
									<uni-forms-item label="产品名称:" label-width="auto" disabled>
										<input v-model="baseFormData.ProductName" type="text" placeholder="自动带出产品名称"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="开工数量:" label-width="auto" required>
										<input v-model="baseFormData.StartQty" type="text" placeholder="开工数量" />
									</uni-forms-item>
									<!-- 下拉 -->
									<uni-forms-item label="设备名称:" label-width="auto" required>
										<view class="uni_forms_item_view" @click="openBaseScreen('机台')">

											<text v-if="!baseFormData.EquipmentName"
												class="u_text">{{FormData.EquipmentName}}</text>
											<text v-else>{{baseFormData.EquipmentName}}</text>
										</view>
									</uni-forms-item>

									<uni-forms-item label="模具名称:" label-width="auto" @click.native='MoludNameclick'>
										<view class="uni_forms_item_view" @click="MoludNameclick">
											<!-- 			<u--text class="u_text" type="info" :text="FormData.MoludName"
												v-if="!baseFormData.MoludName"></u--text> -->
											<text v-if="!baseFormData.MoludName"
												class="u_text">{{FormData.MoludName}}</text>
											<text v-else>{{baseFormData.MoludName}}</text>
										</view>
									</uni-forms-item>

									<uni-forms-item label="工序名称:" label-width="auto" required>
										<input v-model="baseFormData.ProcedureName" type="text" placeholder="请选择工序名称"
											disabled />
										<text class="text" @click="addProcedurname"></text>
									</uni-forms-item>
									<uni-forms-item label="生产车间:" label-width="auto">
										<input v-model="baseFormData.FWorkshopName" type="text" placeholder="自动带出生产车间"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="是否开工:" label-width="auto">
										<input v-model="baseFormData.IsStart?'已开工':'未开工'" type="text"
											placeholder="自动带出是否开工" disabled />
									</uni-forms-item>
									<uni-forms-item label="检验员:" label-width="auto" required>
										<input v-model="baseFormData.QCEmplName" type="text" disabled
											placeholder="请选择检验员" />
										<text class="text" @click="openBaseScreen('人员')"></text>
									</uni-forms-item>
									<uni-forms-item label="开工人:" label-width="auto">
										<input v-model="baseFormData.StartEmplName" type="text" disabled
											placeholder="自动带出开工人" />
									</uni-forms-item>

								</view>
							</uni-forms>



						</view>

					</view>

					<view class="DetailsbottomBtns">

						<button class="button1" block @click="submit">
							{{changeButton}}
						</button>
						<button class="button2" block @click="continueClick">
							<!-- <button class="button2" v-show='ButtonShowTitle' block @click="continueClick"> -->
							继续开工
						</button>
					</view>
					<u-datetime-picker :show="show" v-model="value1" mode="date" @cancel='show=false'
						@confirm='pickerConfirm'></u-datetime-picker>

					<u-action-sheet :actions="showNameList" :show="showName" @select="addName"
						@close="showName=false"></u-action-sheet>

					<u-action-sheet :actions="ProcedurnameList" :show="ProcedurnameShow" @select="ProcedurnameClick"
						@close="ProcedurnameShow=false"></u-action-sheet>
					<u-action-sheet :actions="MoludList" :show="MoludShow" @select="MoludClick"
						@close="MoludShow=false"></u-action-sheet>
					<u-toast ref="uToast"></u-toast>
					<u-popup :show="popup_logout" mode="center" :closeOnClickOverlay="false" :round="4">
						<view class="popup-content">
							<view class="popup-content_close x-c">
								<uni-icons type="closeempty" color="#999" size="20"
									@click='popup_logout = false'></uni-icons>
							</view>
							<view class="x-c title">
								<text>{{popuptitle}}</text>
							</view>
							<view class="contentView">
								<text>{{popupcontent}}</text>
							</view>
							<view class="btnBox x-ac">

								<u-button type="primary" text="确定" @click="confirmClick"></u-button>
							</view>
						</view>
					</u-popup>
				</view>
			</template>

		</mc-framework>
	</view>
</template>
<script>
	import {
		getFormatDateTime,
		getBefore7DateTime
	} from '@/mes/common/base.js';
	export default {
		data() {
			return {
				popuptitle: '',
				popupcontent: '',
				popup_logout: false,

				FormData: {
					MoludName: '请选择模具名称',
					EquipmentName: '请选择机台'
				},

				baseFormData: {
					BillNO: '',
					StartSingleDate: '',
					ProductName: '',
					StartQty: '',
					EquipmentName: '',
					EquipmentCode: '',
					EquipmentID: 0,
					MoludName: '',
					FlowOrder: '',
					ProcedureName: '',
					FWorkshopName: '',
					IsStart: '',
					StartEmplID: '',
					StartEmplName: '',
					QCEmplID: '',
					QCEmplName: '',
					ProductSpec: null,
					ProcessRouteID: null,
					WoPROCID: null,

				},
				sexs: [{
					text: '合格',
					value: 0
				}, {
					text: '不合格',
					value: 1
				}],
				TimeName: '',
				show: false,
				value1: Number(new Date()),
				showName: false,
				showNameList: [],
				changeName: '',
				changeButton: '',
				Equipmentshow: false,
				getEquipmentList: [],
				MoludShow: false,
				MoludList: [],
				ProcedurnameShow: false,
				ProcedurnameList: [],
				ButtonShowTitle: false
			};
		},
		mounted() {},
		onLoad(onload) {
			uni.hideKeyboard(); //隐藏软键盘  
			var item = JSON.parse(onload.item)

			console.log(item);

			if (item.StartSingleID) {
				this.queryList(item.StartSingleID)
			} else {

				this.baseFormData = item
				this.changebuttons(item.StartSingleID)

				this.baseFormData.StartQty = this.baseFormData.StartQty ? this.baseFormData.StartQty : this.baseFormData
					.FPlanQty
				this.baseFormData.MoludID = this.baseFormData.MoludID == null ? 0 : this.baseFormData.MoludID
				this.baseFormData.StartEmplName = this.baseFormData.StartEmplName ? this.baseFormData.StartEmplName : this
					.$store.state.UserInfo.UserName
				this.baseFormData.StartEmplID = this.baseFormData.StartEmplID ? this.baseFormData.StartEmplID : this.$store
					.state.UserInfo.EmplID

				this.baseFormData.StartSingleDate = this.baseFormData.StartSingleDate ? this.baseFormData.StartSingleDate :
					getFormatDateTime(new Date())
				// this.addShowEquipment()
				this.addProcedurnameOne()
			}
		},
		onShow() {
			this.getValue
		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回
				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回
				return true;
			}
		},
		methods: {
			confirmClick() {
				var that = this
				this.popup_logout = false
			},
			addShowEquipment() {
				if (this.baseFormData.EquipmentID) {

					this.baseFormData.EquipmentName = this.baseFormData.EquipmentName
					this.baseFormData.EquipmentID = this.baseFormData.EquipmentID
				} else if (this.baseFormData.JobEquipmentID) {

					this.baseFormData.EquipmentName = this.baseFormData.JobEquipmentName
					this.baseFormData.EquipmentID = this.baseFormData.JobEquipmentID
				} else {

					this.baseFormData.EquipmentName = this
						.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentName
					this.baseFormData.EquipmentID = this
						.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentID
				}

			},

			getValue(e) {
				var that = this;
				console.log(e);
				if (e != '返回') {
					if (e.type == '人员') {
						// that.baseFormData.QCEmplID = e.EmplID
						// that.baseFormData.QCEmplName = e.name
						// console.log(that.baseFormData.QCEmplName);
						that.$set(that.baseFormData, 'QCEmplName', e.name)
						that.$set(that.baseFormData, 'QCEmplID', e.EmplID)
						that.$forceUpdate()
					} else if (e.type == '机台') {
						that.$set(that.baseFormData, 'EquipmentID', e.EquipmentID)
						that.$set(that.baseFormData, 'EquipmentName', e.EquipmentName)

						that.$forceUpdate()
					}

				}
			},

			openBaseScreen(str) {
				uni.hideKeyboard(); //隐藏软键盘
				var that = this;
				var obj;
				if (str == '机台') {
					obj = {

						type: str,
						cmd: 'getEquipment',
						param: {}
					}
				} else if (str == '人员') {
					obj = {

						type: str,
						cmd: 'getEmplByJobName',
						param: {
							JobName: '品质部'
						}
					}
				}

				uni.navigateTo({
					url: `../common/baseScreen?obj=${JSON.stringify(obj)}`,
					animationType: 'slide-in-right',
					animationDuration: 300
				});
			},
			continueClick() {
				var that = this
				var formdata = that.baseFormData
				console.log(formdata);
				formdata.StartSingleID = 0
				formdata.BillNO = '';
				formdata.ProcedureName = ''
				formdata.IsStart = 0
				formdata.EquipmentID = 0
				formdata.EquipmentName = ''
				console.log(formdata);
				this.$forceUpdate()
				this.changebuttons(formdata.StartSingleID)
			},
			MoludClick(e) {
				var that = this
				console.log(e);
				this.baseFormData.MoludName = e.name
				this.baseFormData.MoludID = e.MoludID
				this.MoludShow = false
				this.$forceUpdate()
			},
			MoludNameclick() {
				var that = this
				that.Request.request('PGD_GetMolud', {
					ProductID: that.baseFormData.ProductID
				}, (ret) => {
					console.log(ret.data);
					if (ret.status == 200) {
						if (ret.data.ds[0].status == 500) {

							that.popuptitle = '报错'
							that.popupcontent = ret.data.ds[0].msg
							that.popup_logout = true
						} else {
							ret.data.ds.map(item => item.name = item.MoludCode)
							that.MoludList = ret.data.ds
							that.MoludShow = true
						}
					} else {
						that.popuptitle = '报错'
						that.popupcontent = ret.data.ds[0].msg
						that.popup_logout = true
					}

				})
			},
			// 工序 
			ProcedurnameClick(e) {
				var that = this

				// that.baseFormData.QCEmplID = e.EmplID
				that.baseFormData = {
					...that.baseFormData,
					...e
				}
				if (e.EquipmentID) {
					that.baseFormData.EquipmentID = e.EquipmentID
					that.baseFormData.EquipmentName = e.EquipmentName
				} else {
					that.baseFormData.EquipmentName = that
						.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentName
					that.baseFormData.EquipmentID = that
						.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentID
				}

				that.baseFormData.MoludID = that.baseFormData.MoludID == null ? 0 : that.baseFormData.MoludID
				that.baseFormData.IsStart = ''
				console.log(that.baseFormData);
				that.$forceUpdate()
				that.ProcedurnameShow = false

			},
			addProcedurname(str) {
				var that = this;
				var obj = {
					EquipmentID: 0,
					IsStart: 0,
					JobOrderID: this.baseFormData.JobOrderID,
				}
				that.Request.request('getJobOrderPROC', obj, function(ret) {
					console.log(ret);
					if (ret.status == 200) {
						ret.data.ds.map(item => item.name = item.ProcedureName)
						that.ProcedurnameList = ret.data.ds
						that.ProcedurnameShow = true
					}
				})
			},
			// 第一次进入
			addProcedurnameOne() {
				var that = this;
				var obj = {
					EquipmentID: 0,
					IsStart: 0,
					JobOrderID: this.baseFormData.JobOrderID,
				}
				that.Request.request('getJobOrderPROC', obj, function(ret) {
					console.log(ret);
					if (ret.status == 200) {
						var Procedure = ret.data.ds.filter(item => item.ProcedureName == that.baseFormData
							.ProcedureName)

						if (Procedure[0].EquipmentID) {
							that.baseFormData.EquipmentID = Procedure[0].EquipmentID
							that.baseFormData.EquipmentName = Procedure[0].EquipmentName
						} else {
							that.baseFormData.EquipmentName = that
								.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentName
							that.baseFormData.EquipmentID = that
								.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentID
						}
						that.$forceUpdate()
					}
				})
			},
			addName(e) {
				var that = this
				console.log(e);
				that.baseFormData.QCEmplID = e.EmplID
				that.baseFormData.QCEmplName = e.EmplName
				that.showName = false
				this.$forceUpdate()
			},
			// Equipmentopen() {
			// 	var that = this
			// 	// console.log('open');
			// 	this.Request.request('getEquipment', {}, (ret) => {
			// 		console.log(JSON.stringify(ret))
			// 		if (ret.status == 200) {
			// 			that.getEquipmentList = ret.data.ds
			// 		}
			// 	})
			// },
			// Equipmentclose() {
			// 	this.Equipmentshow = false
			// },
			// Equipmentclosetow(item) {
			// 	console.log(item);
			// 	this.baseFormData.EquipmentName = item.EquipmentName
			// 	this.baseFormData.EquipmentCode = item.EquipmentCode
			// 	this.baseFormData.EquipmentID = item.EquipmentID
			// 	this.Equipmentclose()
			// 	this.$forceUpdate()
			// },
			// 设备
			// changeEquipment() {
			// 	this.Equipmentshow = !this.Equipmentshow
			// },
			changebuttons(ID) {
				var that = this;
				if (ID == 0) {
					that.changeButton = '开工'
				} else {
					that.changeButton = '保存'
					that.Request.request('IfShowKGButton', {
						JobOrderID: that.baseFormData.JobOrderID
					}, (res) => {
						console.log(res.data.ds);
						if (res.status == 200) {
							if (res.data.ds[0].CC > 0) {
								that.ButtonShowTitle = true
							} else {
								that.ButtonShowTitle = false
							}
						}
					})

				}
			},

			// 获取人员
			openBase(str) {
				var that = this;
				var obj = {}
				var cmd
				if (str == '检验员') {
					that.changeName = str
					cmd = 'getEmplByJobName'
					obj.JobName = '品质部'
				}
				that.Request.request(cmd, obj, function(ret) {
					console.log(ret);
					if (ret.status == 200) {
						that.showName = true
						ret.data.ds.map(item => item.name = item.EmplName)
						that.showNameList = ret.data.ds
					}
				})
			},
			// 获取时间
			pickerConfirm(e) {
				var time = getFormatDateTime(e.value)
				this.baseFormData.StartSingleDate = time
				this.show = false
				this.$forceUpdate()
			},

			// 保存
			submit() {
				var that = this;
				var show = true
				var msg

				if (!that.baseFormData.QCEmplName) {
					msg = '请输入检验员'
					show = false
				} else if (that.baseFormData.StartQty == '' || that.baseFormData.StartQty == null) {
					msg = '请输入开工数量'
					show = false
				} else if (!that.baseFormData.EquipmentName) {
					msg = '请输入设备名称'
					show = false
				} else if (!that.baseFormData.ProcedureName) {
					msg = '请输入工序'
					show = false
				}
				console.log(this.baseFormData);
				if (show) {
					if (that.baseFormData.MoludID == null || that.baseFormData.MoludID == '') {
						that.baseFormData.MoludID = 0
					}
					that.Request.request('saveStartOrder', that.baseFormData, function(ret) {
						console.log(ret);

						if (ret.status == 200) {
							if (ret.data.ds[0].status == 500) {
								that.popuptitle = '报错'
								that.popupcontent = ret.data.ds[0].msg
								that.popup_logout = true
							} else {
								that.$refs.uToast.show({
									title: '保存',
									type: 'success',
									message: '保存成功'
								})
								setTimeout(() => {
									that.queryList(ret.data.ds[0].StartSingleID)
								}, 1000)
							}
						}
					})

				} else {


					that.popuptitle = '提示'
					that.popupcontent = msg
					that.popup_logout = true

				}

			},
			queryList(id) {
				var that = this;
				that.Request.request('getStartOrderDetail', {
					StartSingleID: id
				}, (ret) => {
					console.log(ret.data.ds);
					if (ret.status == 200) {
						that.baseFormData = ret.data.ds[0]
						that.baseFormData.StartSingleDate = that.baseFormData.StartSingleDate ? that.baseFormData
							.StartSingleDate.split('T')[0] : ''
						that.baseFormData.MoludID = that.baseFormData.MoludID == null ? 0 : that.baseFormData
							.MoludID


						that.$forceUpdate()
						that.changebuttons(that.baseFormData.StartSingleID)
					}
				})
			},
			// 跳转tag
			changeLevel(data, row) {
				let routes = getCurrentPages(); //当前页面栈
				let backIndex = routes.length - row.length; // 计算返回页面栈数

				if (backIndex == 1) {
					let prevPage = routes[routes.length - 2]
					prevPage.$vm.getList()
				}
				uni.navigateBack({
					delta: backIndex
				})
			},
		}
	}
</script>

<style lang="scss">
	@import "../css/index.css";

	.form-item-row {
		display: flex;
		flex-wrap: wrap;
	}
</style>